package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.supports.PageData;
import com.zjcode.cdcer.config.MConsts;
import com.zjcode.cdcer.enums.CommonEnum;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.model.CerDetaildataImportOfficeInfo;
import com.zjcode.cdcer.model.CerEndoscopicDiagnosisDetails;
import com.zjcode.cdcer.service.CerDetaildataImportOfficeInfoService;
import com.zjcode.cdcer.service.CerEndoscopicDiagnosisDetailsService;
import com.zjcode.cdcer.utils.Utils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/v1/detaileddatareportingOffice/")
public class CerDetaildataImportOfficeInfoController extends BaseController {

    @Autowired
    private CerDetaildataImportOfficeInfoService cerDetaildataImportOfficeInfoService;

    @Autowired
    private CerEndoscopicDiagnosisDetailsService cerEndoscopicDiagnosisDetailsService;

    @PostMapping("batchInsert") 
    @ApiOperation("存入正式库")
    public JsonObj batchInsert(@RequestBody PageData pageData) {
        return cerDetaildataImportOfficeInfoService.batchInsert(pageData);
    }

    @PostMapping("getOfficePageData")
    @ApiOperation("获取所有的详细数据信息列表")
    public ListResult<Object> listByPage(@RequestBody PageData pageData) {
        return ResultBuilder.buildListSuccess(cerDetaildataImportOfficeInfoService.getOfficePageData(pageData));
    }

    @PostMapping("queryAllOfficeExportData")
    @ApiOperation("点击导出按钮，获取所有的详细数据信息列表")
    public JsonObj queryAllOfficeExportData(@RequestBody PageData pageData) {
        List<LinkedHashMap<String,Object>> list  = cerDetaildataImportOfficeInfoService.queryAllOfficeExportData(
                pageData);
        Map<String,Object> map = new HashMap<>();
        map.put("data",list);
        return JsonObj.success(map);
    }

    @GetMapping("findDataById")
    @ApiOperation("根据id获取详细信息")
    public JsonObj findDataById() {
        PageData pageData = getPageData();
        String id = (String) pageData.get("id");
        CerDetaildataImportOfficeInfo info = cerDetaildataImportOfficeInfoService.findDataById(id);
        List<CerEndoscopicDiagnosisDetails> list = cerEndoscopicDiagnosisDetailsService.findByParentId(
                id);
        if (info != null) {
            String extFlag2 = info.getExtFlag2();
            Map<String, Boolean> map = Utils.parseExtFlag2(extFlag2);
            info.setIsShowSgDetail(map.get(MConsts.SUB_DETAIL.SG) ? 1 : 0);
            info.setIsShowBmDetail(map.get(MConsts.SUB_DETAIL.BM) ? 1 : 0);
            info.setIsShowWDetail(map.get(MConsts.SUB_DETAIL.WD) ? 1 : 0);
            info.setReportFilePath("");
            info.setBlReportFilePath("");

            Map<String, Object> data = new HashMap<>();
            Map<String, List<CerEndoscopicDiagnosisDetails>> detailMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(list)) {
                detailMap = list
                        .parallelStream()
                        .collect(Collectors.groupingBy(CerEndoscopicDiagnosisDetails::getObjType));
            }
            data.put(CommonEnum.RESP.getValue(), info);
            data.put("detailMap", detailMap);
            return JsonObj.success(data);
        } else {
            return JsonObj.error("未能获取到对应的数据");
        }
    }
}
